// RightForm.cpp : implementation file
//

#include "stdafx.h"
#include "DAD.h"
#include "RightForm.h"
#include "MyObject1.h"
#include <math.h>

using namespace RealtimeCurve;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRightForm

IMPLEMENT_DYNCREATE(CRightForm, CFormView)

CRightForm::CRightForm()
	: CFormView(CRightForm::IDD)
{
	m_time = 0.0f;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//{{AFX_DATA_INIT(CRightForm)
	//}}AFX_DATA_INIT
}

CRightForm::~CRightForm()
{
}

void CRightForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRightForm)
	DDX_Control(pDX, IDC_PLOT, m_plot);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRightForm, CFormView)
	//{{AFX_MSG_MAP(CRightForm)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_START, OnBnClickedStart)
	ON_BN_CLICKED(IDC_STOP, OnBnClickedStop)
	ON_WM_SIZE()
	ON_WM_SIZING()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRightForm diagnostics

#ifdef _DEBUG
void CRightForm::AssertValid() const
{
	CFormView::AssertValid();
}

void CRightForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRightForm message handlers

void CRightForm::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	GetWindowRect(&m_rectOldWindow);
	m_plot.GetWindowRect(&m_rectOldPlotWindow);
	TRACE("m_rectOldPlotWindow.left=%d,m_rectOldPlotWindow.width=%d\n", m_rectOldPlotWindow.left, m_rectOldPlotWindow.Width());
	ScreenToClient(&m_rectOldPlotWindow);
	TRACE("m_rectOldPlotWindow.left=%d,m_rectOldPlotWindow.width=%d\n", m_rectOldPlotWindow.left, m_rectOldPlotWindow.Width());

	m_plot.AddNewLine(_T("PV"), PS_SOLID, RGB(255, 0, 0));
	m_plot.AddNewLine(_T("SV"), PS_SOLID, RGB(0, 255, 0));

	m_plot.GetAxisX().AxisColor = RGB(0, 255, 0);
	m_plot.GetAxisY().AxisColor = RGB(0, 255, 0);

	OnSize(0,0,0);

	m_plot.Initialize(&m_plot);

}

void CRightForm::OnSysCommand(UINT nID, LPARAM lParam)
{
	CFormView::OnSysCommand(nID, lParam);
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRightForm::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CFormView::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRightForm::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CRightForm::OnTimer(UINT nIDEvent)
{

	//In fact, x values are not used when draw lines
	//If you want to use x values in any way, 
	//you may calculate point x in the way that point y is calculated.
	//see also: DrawLines(CDC* pDC);
	float  y0 = 500 * sin(m_time / 5.0f);
	float y1 = 500 * cos(m_time / 10.0f);

	m_plot.AddNewPoint(m_time, y0, 0);
	m_plot.AddNewPoint(m_time, y1, 1);
	m_time += 0.20f;
	//m_plot.GetAxisX().GetRangeLowerLimit() += 0.10f; 
	//CDialog::OnTimer(nIDEvent);
}

void CRightForm::OnBnClickedStart()
{
	SetTimer(1, 10, NULL);
	m_plot.SetRate(10);//
	m_plot.Start();

	GetDlgItem(IDC_START)->EnableWindow(FALSE);
}

void CRightForm::OnBnClickedStop()
{
	KillTimer(1);
	m_plot.Stop();
	//for(int i=0;i<m_plot.GetLineCount();i++)
	//{
	//	m_plot.GetLineByIndex(i).RemoveAllPoints();
	//}
	GetDlgItem(IDC_START)->EnableWindow();
	m_plot.Invalidate();
}

/*
 Set new control postition as needed.
 */
void CRightForm::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);
	CRect rect;
	GetClientRect(&rect);


	
	if (m_plot.GetSafeHwnd())
	{
		//TRACE("m_rectOldPlotWindow.left=%d,m_rectOldPlotWindow.width=%d\n",m_rectOldPlotWindow.left,m_rectOldPlotWindow.Width());

		m_plot.MoveWindow(m_rectOldPlotWindow.left,
			m_rectOldPlotWindow.top,
			rect.right - 5 - m_rectOldPlotWindow.left,
			rect.bottom -  7 - m_rectOldPlotWindow.top);
	}


}
/*
 Check window size while resizeing, if small than origianl size, then restore to original size.
 */
void CRightForm::OnSizing(UINT fwSide, LPRECT pRect)
{
	/*if(pRect->right - pRect->left <=m_rectOldWindow.Width())
		pRect->right = pRect->left + m_rectOldWindow.Width();

	 if(pRect->bottom - pRect->top <=m_rectOldWindow.Height())
		pRect->bottom = pRect->top + m_rectOldWindow.Height();*/

	CFormView::OnSizing(fwSide, pRect);
}